import Mock from 'mockjs'


let userData=[{
    id:1,
    date: '2016-05-02',
    name: '李小虎',
    address: '上海市普陀区金沙江路 1518 弄'
}, {
    id:2,
    date: '2016-05-04',
    name: '王小虎',
    address: '上海市普陀区金沙江路 1517 弄'
}, {
    id:3,
    date: '2016-05-01',
    name: '孙小虎',
    address: '上海市普陀区金沙江路 1518 弄'
}, {
    id:4,
    date: '2016-05-03',
    name: '张小虎',
    address: '上海市普陀区金沙江路 1519 弄'
}]
//fixed: 修复了添加用户时，id的问题
let id=userData.length+1
export default {
    getUserData:()=>{


        return {
            code:20000,
            data:userData
        }
    },
    addUser:(user)=>{
        user.id=id++
        userData.push(user)

        return {
            code:20000,
            message:'添加成功'
        }
    },
    deleteUser:(user)=>{
        //找到该用户在数组中的索引
        const index=userData.findIndex(item=>{
            return item.name===user.name
        })
        //根据索引进行删除
        userData.splice(index,1);
        return {
            code:20000,
            message:'删除成功',
            data:userData
        }
    },
    editUser:(user)=>{
        userData.splice(userData.findIndex(item=>{
            return item.id===user.id
        }),1,user)

        return {
            code:20000,
            message:'编辑成功',
            data:userData
        }
    }
}
